package com.jinxin.platform.cdinfogo.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.cdinfogo.pojo.vo.WeatherInfo;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;

/**
 * @author : zhangpeng
 * @date : 2021-07-09 11:07:25
 **/
public class WeatherUtils {

    /**
     * 通过城市名称获取该城市的天气信息
     *
     * @param cityName
     * @return
     */

    public  static WeatherInfo GetWeatherData(String cityname) {
        StringBuilder sb=new StringBuilder();;
        try {
            //cityname = URLEncoder.encode(cityName, "UTF-8");
            String weather_url = "https://restapi.amap.com/v3/weather/weatherInfo?city="+cityname+"&key=cc2fd7e0981ad56eae4f7b634a8d4bb1";


            URL url = new URL(weather_url);
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();
//            GZIPInputStream gzin = new GZIPInputStream(is);
            InputStreamReader isr = new InputStreamReader(is, "utf-8"); // 设置读取流的编码格式，自定义编码
            BufferedReader reader = new BufferedReader(isr);
            String line = null;
            while((line=reader.readLine())!=null)
                sb.append(line+" ");
            reader.close();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //System.out.println(sb.toString());
        return GetWeather(sb.toString());

    }


    /**
     * 将JSON格式数据进行解析 ，返回一个weather对象
     * @param str
     * @return
     */
    public static WeatherInfo GetWeather(String weatherInfobyJson){
        System.out.println(weatherInfobyJson);
        JSONObject dataOfJson = JSONObject.parseObject(weatherInfobyJson);
        if(!dataOfJson.getString("status").equals("1")) return null;

        //创建WeatherInfo对象，提取所需的天气信息
        WeatherInfo weatherInfo = new WeatherInfo();

        //从json数据中提取数据
//        String data = dataOfJson.getString("lives");
//
//        dataOfJson = JSONObject.parseObject(data);
//        weatherInfo.setCityname(dataOfJson.getString("city"));
//        System.out.println("1111111111111111111111111");
//        weatherInfo.setAirquality(dataOfJson.getString("aqi"));
//        System.out.println("2222222222222222222222222");
        //获取预测的天气预报信息
        JSONArray forecast = dataOfJson.getJSONArray("lives");
        //取得当天的
        JSONObject result=forecast.getJSONObject(0);

//        weatherInfo.setDate(result.getString("date"));
//
//        String high = result.getString("high").substring(2);
//        String low  = result.getString("low").substring(2);
//
//        weatherInfo.setTemperature(low+"~"+high);

        weatherInfo.setWeather(result.getString("type"));
        weatherInfo.setTemperature(result.getString("temperature"));
        weatherInfo.setWeather(result.getString("weather"));
        weatherInfo.setHumidity(result.getString("humidity"));
        weatherInfo.setWindDirection(result.getString("winddirection"));



        return weatherInfo;
    }


//    public static void main(String[] args) {
//        String cd = GetWeatherData("110101");
//        System.out.println(GetWeather(cd));
//    }

}
